﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpArch.Core.DomainModel;

namespace Peak.Core
{
  public class Calendar : Entity
  {
    public virtual bool Monday { get; set; }
    public virtual bool Tuesday { get; set; }
    public virtual bool Wednesday { get; set; }
    public virtual bool Thursday { get; set; }
    public virtual bool Friday { get; set; }
    public virtual bool Saturday { get; set; }
    public virtual bool Sunday { get; set; }
    public virtual TimeSpan StartTime { get; set; }
    public virtual TimeSpan EndTime { get; set; }
    public virtual IList<Holiday> Holidays { get; set; }

    public Calendar()
    {
      Monday = Tuesday = Wednesday = Thursday = Friday = true;
      Sunday = Saturday = false;
      StartTime = new TimeSpan(0,8,0,0);
      EndTime = new TimeSpan(0, 16, 0, 0);
    }

    public virtual bool IsWorkingDay(DateTime date)
    {      
      if(date.DayOfWeek == DayOfWeek.Monday && !Monday)
        return false;
      if (date.DayOfWeek == DayOfWeek.Tuesday && !Tuesday)
        return false;
      if (date.DayOfWeek == DayOfWeek.Wednesday && !Wednesday)
        return false;
      if (date.DayOfWeek == DayOfWeek.Thursday && !Thursday)
        return false;
      if (date.DayOfWeek == DayOfWeek.Friday && !Friday)
        return false;
      if (date.DayOfWeek == DayOfWeek.Saturday && !Saturday)
        return false;
      if (date.DayOfWeek == DayOfWeek.Sunday && !Sunday)
        return false;

      foreach(Holiday holiday in Holidays)
      {
        if(date.Date >= holiday.Start.Date && date.Date <= holiday.End.Date)
          return false;
      }

      return true;
    }
  }
}
